<?php

namespace app\traits;

use think\facade\Cache;
use think\helper\Arr;

trait ServiceTrait
{
    /**
     * 接口返回每页条数参数名
     * @param $scene
     * @return string
     */
    public function listRowsName($scene): string
    {
        $maps = [
            'api' => 'limit',
            'system' => 'limit'
        ];
        return Arr::get($maps, $scene);
    }

    public function toTree(array $data = [], int $parentId = 0, string $id = 'id', string $parentField = 'parent_id', string $children = 'children'): array
    {
        if (empty($data)) {
            return [];
        }

        $tree = [];

        foreach ($data as $value) {
            if ($value[$parentField] == $parentId) {
                $child = $this->toTree($data, $value[$id], $id, $parentField, $children);
                if (!empty($child)) {
                    $value[$children] = $child;
                }
                array_push($tree, $value);
            }
        }

        unset($data);
        return $tree;
    }
}